package com.baizhi.controller;

import com.baizhi.entity.Banner;
import com.baizhi.entity.Guru;
import com.baizhi.service.BannerService;
import com.baizhi.serviceplus.BannerServicePlus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/banner")
@ResponseBody
public class BannerController {
    @Autowired
    private BannerService bannerService;
    @Autowired
    private BannerServicePlus bannerServicePlus;


    //全查分页
    @RequestMapping("/selectAllBanner")
    public Map selectAllBanner(Integer page,Integer limit){
        Map map = bannerService.selectAll(page, limit);
        return map;
    }

    //删除
    @RequestMapping("/deleteBanner")
    public Map deleteBanner(Integer id){
        System.out.println("正在删除轮播图"+id);
        Map map = bannerServicePlus.deleteBanner(id);
        return map;
    }

    //添加
    @RequestMapping("/insertBanner")
    public Map insert(MultipartFile file, Banner banner, HttpServletRequest request) throws IOException {
        System.out.println("轮播图片上传中-----");
        System.out.println(banner);
        //获取文件要上传的地址
        String realPath = request.getServletContext().getRealPath("/cmfz/img");
        //获取文件名
        String originalFilename = new Date().getTime()+"_"+file.getOriginalFilename();
        //存入数据库的名称
        String newFileName="/cmfz/img"+"/"+originalFilename;
        //存入数据库
        banner.setBannerImageUrl(newFileName);
        System.out.println(banner);
        //文件上传到对应的地址
        file.transferTo(new File(realPath+"/"+originalFilename));
        System.out.println("-----------------------------------------");
        System.out.println(banner);
        Map map = bannerServicePlus.insertBanner(banner);
        return map;
    }

    //修改
    @RequestMapping("/updateBanner")
    public Map update(MultipartFile file, Banner banner, HttpServletRequest request) throws IOException {
       if (!"".equals(file.getOriginalFilename())) {
           System.out.println("轮播图片修改中-----");
           System.out.println(banner);
           //获取文件要上传的地址
           String realPath = request.getServletContext().getRealPath("/cmfz/img");
           //获取文件名
           String originalFilename = new Date().getTime() + file.getOriginalFilename();
           //存入数据库的名称
           String newFileName = "/cmfz/img" + "/" + originalFilename;
           //存入数据库
           banner.setBannerImageUrl(newFileName);
           System.out.println(banner);
           //文件上传到对应的地址
           file.transferTo(new File(realPath + "/" + originalFilename));
           System.out.println("-----------------------------------------");
           System.out.println(banner);
       }
        Map map = bannerServicePlus.updateBanner(banner);
        return map;
    }

    @RequestMapping("/updateBannerStatus")
        public Map  updateBannerStatus(Integer status,Integer id){
        Map map = bannerServicePlus.updateBannerStatus(status, id);
        return  map;
    }
}
